package com.ld.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ld.pojo.Student;
import com.ld.service.StudentService;
import com.ld.util.PageResult;
import com.ld.util.R;
import com.ld.vo.StudentVo;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("student")
public class StudentController {

	@Reference
	private StudentService studentService;

	//1. 查询所有学生列表
	@GetMapping("list")
	public List<Student> findAll(){
		return studentService.findAll();
	}

	//2. 得到vue需要的学生列表
	@GetMapping("findAll")
	public R findAll2(){
		return R.ok().data("list",studentService.findAll());
	}

	//3. 分页查询（不带条件）
	@GetMapping("findByPage/{page}/{pagesize}")
	public R findByPage(@PathVariable int page,@PathVariable int pagesize){
		//3.1 分页查询出分页对象
		PageResult<Student> pr = studentService.findByPage(page,pagesize);
		//3.2 返回
		return R.ok().data("pr",pr);
	}

	//4. 条件查询带分页
	@PostMapping("search/{page}/{pagesize}")
	public R search(@PathVariable int page,@PathVariable int pagesize,@RequestBody StudentVo vo){
		//4.1 分页查询出分页对象
		PageResult<Student> pr = studentService.search(page,pagesize,vo);
		//4.2 返回
		return R.ok().data("pr",pr);
	}
	//2. 添加学生
	@PostMapping("add")
	public String add(@RequestBody Student student){
		studentService.add(student);
		return "add";
	}
}
